<?php
	global $AppUI, $project_id, $df, $canEdit, $m, $tab;

	// Lets check which cost codes have been used before
	$q  = new DBQuery;
	$q->addQuery('project_company');
	$q->addTable('projects');
	$q->addWhere('project_id = ' . $project_id);
	$company_id = $q->loadResult();

	$q->addTable('billingcode');
	$q->addQuery('billingcode_id, billingcode_name');
	$q->addOrder('billingcode_name');
	$q->addWhere('billingcode_status = 0');
	$q->addWhere('(company_id = 0 OR company_id = ' . $company_id . ')');
	$task_log_costcodes = $q->loadHashList();

 	// Show deleted codes separately (at the end)
	$q->addTable('billingcode');
	$q->addQuery('billingcode_id, billingcode_name');
	$q->addOrder('billingcode_name');
	$q->addWhere('billingcode_status = 1');
	$q->addWhere('(company_id = 0 OR company_id = ' . $company_id . ')');
	$task_log_costcodes = array_merge(array(0 => 'None'), $task_log_costcodes, $q->loadHashList());

	$q  = new DBQuery;
	$q->addTable('users', 'u');
	$q->addQuery('user_id, concat(contact_first_name," ",contact_last_name)');
	$q->addJoin('contacts', 'c', 'u.user_contact = c.contact_id');
	$q->addJoin('project_contacts', 'p', 'p.contact_id = u.user_id');
	$q->addOrder('contact_first_name, contact_last_name');
	$q->addWhere('p.project_id = '.$project_id);
	$users = arrayMerge( array( '-1' => $AppUI->_('All members') ), $q->loadHashList() );

	$cost_code = dPgetParam( $_GET, 'cost_code', '0' );

	if (isset( $_GET['user_id'] )) {
		$AppUI->setState( 'ProjectsTaskLogsUserFilter', $_GET['user_id'] );
	}
	$user_id = $AppUI->getState( 'ProjectsTaskLogsUserFilter' ) ?
				$AppUI->getState( 'ProjectsTaskLogsUserFilter' ) : $AppUI->user_id;

	if (isset( $_GET['hide_inactive'] )) {
		$AppUI->setState( 'ProjectsTaskLogsHideArchived', true );
	} else {
		$AppUI->setState( 'ProjectsTaskLogsHideArchived', false );
	}
	$hide_inactive = $AppUI->getState( 'ProjectsTaskLogsHideArchived' );

	if (isset( $_GET['hide_complete'] )) {
		$AppUI->setState( 'ProjectsTaskLogsHideComplete', true );
	} else {
		$AppUI->setState( 'ProjectsTaskLogsHideComplete', false );
	}
	$hide_complete = $AppUI->getState( 'ProjectsTaskLogsHideComplete' );

?>
<script language="JavaScript">
<? if ($canEdit) { ?>
function doDeleteTaskLog(id)
{
	if (confirm( "<?=$AppUI->_('doDelete', UI_OUTPUT_JS).' '.$AppUI->_('Task Log', UI_OUTPUT_JS).'?'?>" )) {
		document.frmLogDelete.task_log_id.value = id;
		document.frmLogDelete.submit();
	}
}
<? } ?>

function doShowTaskLog(task, log, tab)
{
	go('?m=tasks&a=view&task_id='+task+'&tab='+tab+'&task_log_id='+log);
}
</script>

<table border="0" cellpadding="2" cellspacing="0" width="100%" class="infopanel">
<form name="frmFilter" action="index.php" method="get">
<tr>
	<td width="98%">&nbsp;</td>
	<td width="1%">
		<input id="idHIA" type="checkbox" name="hide_inactive" <?=$hide_inactive?'checked':''?>
			onchange="document.frmFilter.submit()">
	</td>
	<td nowrap="nowrap"><label for="idHIA"><?=$AppUI->_('Hide Inactive')?></label></td>
	<td width="1%">
		<input id="idHC" type="checkbox" name="hide_complete" <?=$hide_complete?'checked':''?>
			onchange="document.frmFilter.submit()">
	</td>
	<td nowrap="nowrap"><label for="idHC"><?=$AppUI->_('Hide 100% Complete')?></label></td>
	<td><img src="images/blank.gif" width="16"></td>
	<td width="1%" nowrap="nowrap"><?=$AppUI->_('User')?></td>
	<td width="1%">
	<?=arraySelect( $users, 'user_id', 'size="1" class="text" onchange="document.frmFilter.submit()"', $user_id )?>
	</td>
	<td width="1%" nowrap="nowrap">&nbsp;<?=$AppUI->_('Cost Code')?></td>
	<td width="1%">
	<?
		echo arraySelect( $task_log_costcodes, 'cost_code',
				'size="1" class="text" onchange="document.frmFilter.submit()"', $cost_code );
	?>
	</td>
	<td><img src="images/blank.gif" width="8"></td>
</tr>
<input type="hidden" name="m" value="projects"/>
<input type="hidden" name="a" value="view"/>
<input type="hidden" name="project_id" value="<?=$project_id?>"/>
<input type="hidden" name="tab" value="<?=$tab?>"/>
</form>
</table>
<table border="0" cellpadding="3" cellspacing="0" width="100%" class="list">
<form name="frmLogDelete" action="index.php?m=tasks" method="post">
	<input type="hidden" name="dosql" value="do_updatetask">
	<input type="hidden" name="del" value="1" />
	<input type="hidden" name="task_log_id" value="0" />
</form>
<tr>
	<th></th>
	<th><?=$AppUI->_('Date')?></th>
	<th width="100"><?=$AppUI->_('Summary')?></th>
	<th width="100"><?=$AppUI->_('User')?></th>
	<th width="100"><?=$AppUI->_('Hours')?></th>
	<th width="100"><?=$AppUI->_('CC')?></th>
	<th width="100%"><?=$AppUI->_('Comments')?></th>
	<th></th>
</tr>
<?php
// Winnow out the tasks we are not allowed to view.
$perms =& $AppUI->acl();
$project =& new CProject;

// Pull the task comments
$q  = new DBQuery;
$q->addTable('task_log');
$q->addQuery('task_log.*, user_username, task_id');
$q->addQuery('billingcode_name as task_log_costcode');
$q->addJoin('users', 'u', 'user_id = task_log_creator');
$q->addJoin('tasks', 't', 'task_log_task = t.task_id');
$q->addJoin('billingcode', 'b', 'task_log.task_log_costcode = billingcode_id');
$q->addWhere('task_project = '.$project_id);
if ($user_id>0)
	$q->addWhere('task_log_creator = '.$user_id);
if ($hide_inactive)
	$q->addWhere('task_status >= 0');
if ($hide_complete)
	$q->addWhere('task_percent_complete < 100');
if ($cost_code != '0')
	$q->addWhere('task_log_costcode = "'.$cost_code.'"');
$q->addOrder('task_log_date');
$project->setAllowedSQL($AppUI->user_id, $q, 'task_project');
$logs = $q->loadList();

$s = '';
$hrs = 0;
if (count($logs) == 0) {
	showEmptyRow(8, 'second');
}
else
foreach ($logs as $row) {
	$mylog = $row['task_log_creator'] == $AppUI->user_id;
	$task_log_date = intval( $row['task_log_date'] ) ? new CDate( $row['task_log_date'] ) : null;
?>
<tr class="second">
<td>
<?
	if ($perms->checkModuleItem('tasks', 'edit', $row['task_id']) && $mylog ) {
		showIconLink('images/edit_log.gif', 'js:doShowTaskLog('.$row['task_id'].','.$row['task_log_id'].',1)', 'Edit');
	}
	else {
		showIconLink('images/view.gif', 'js:doShowTaskLog('.$row['task_id'].','.$row['task_log_id'].',0)', 'View');
	}
?>
</td>
<td nowrap="nowrap"><?=$task_log_date ? $task_log_date->format( $df ) : '-'?></td>
<td width="30%"><a href="?m=tasks&a=view&task_id=<?=$row['task_id']?>&tab=0"><?=$row['task_log_name']?></a></td>
<td width="100"><?=$row['user_username']?></td>
<td width="100" align="right"><?=sprintf( '%.2f', $row['task_log_hours'] )?></td>
<td width="100"><?=$row['task_log_costcode']?></td>
<td><?=str_replace( "\n", '<br/>', $row['task_log_description'] )?></td>
<td>
<?
	if ($perms->checkModuleItem('tasks', 'delete', $row['task_id']) && $mylog) {
		showIconLink('images/delete_log.gif', 'js:doDeleteTaskLog('.$row['task_log_id'].')', 'Delete');
	}
	else {
		echo '<img src="images/nodel_log.gif" title="No delete">';
	}
?>
</td>
</tr>
<?
	$hrs += (float)$row['task_log_hours'];
}
?>
<tr bgcolor="white" valign="top">
<td colspan="4" align="right"><?=$AppUI->_('Total Hours')?></td>
<td align="right"><?=sprintf( '%.2f', $hrs )?></td>
<td colspan="100%">&nbsp;</td>
</tr>
</table>
